#!/bin/bash

# 彩票记录管理应用 deb 打包脚本

set -e  # 遇到错误时退出

# 项目信息
PACKAGE_NAME="gambling-memories"
PACKAGE_ARCH="all"
MAINTAINER="洗衣带娃 <a343025902@qq.com>"
DESCRIPTION="彩票记录管理应用，支持双色球和大乐透号码记录与中奖匹配"

# 版本号自增函数
increment_version() {
    local version_file="VERSION"
    local current_version=""
    
    # 如果版本文件存在，读取当前版本号
    if [ -f "$version_file" ]; then
        current_version=$(cat "$version_file")
    else
        # 如果版本文件不存在，初始化为1.0.0
        current_version="1.0.0"
    fi
    
    # 解析版本号
    IFS='.' read -ra VERSION_PARTS <<< "$current_version"
    major=${VERSION_PARTS[0]}
    minor=${VERSION_PARTS[1]}
    patch=${VERSION_PARTS[2]}
    
    # 递增补丁版本号
    patch=$((patch + 1))
    
    # 组合新的版本号
    new_version="${major}.${minor}.${patch}"
    
    # 保存新版本号到文件
    echo "$new_version" > "$version_file"
    
    echo "$new_version"
}

# 获取自增后的版本号
PACKAGE_VERSION=$(increment_version)
echo "当前打包版本: $PACKAGE_VERSION"

# 创建打包目录结构
echo "创建deb打包目录结构..."
BUILD_DIR="deb_build"
PACKAGE_DIR="$BUILD_DIR/$PACKAGE_NAME-$PACKAGE_VERSION"
DEBIAN_DIR="$PACKAGE_DIR/DEBIAN"
APP_DIR="$PACKAGE_DIR/usr/share/$PACKAGE_NAME"
BIN_DIR="$PACKAGE_DIR/usr/bin"
APP_DATA_DIR="$PACKAGE_DIR/usr/share/applications"
ICON_DIR="$PACKAGE_DIR/usr/share/icons/hicolor/scalable/apps"

mkdir -p "$DEBIAN_DIR"
mkdir -p "$APP_DIR"
mkdir -p "$BIN_DIR"
mkdir -p "$APP_DATA_DIR"
# 创建不同尺寸的图标目录
mkdir -p "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps"
mkdir -p "$PACKAGE_DIR/usr/share/icons/hicolor/22x22/apps"
mkdir -p "$PACKAGE_DIR/usr/share/icons/hicolor/32x32/apps"
mkdir -p "$PACKAGE_DIR/usr/share/icons/hicolor/48x48/apps"
mkdir -p "$PACKAGE_DIR/usr/share/icons/hicolor/64x64/apps"
mkdir -p "$PACKAGE_DIR/usr/share/icons/hicolor/128x128/apps"
mkdir -p "$ICON_DIR"

# 复制应用文件
echo "复制应用文件..."
cp -r app.gambling-memories/* "$APP_DIR/"

# 复制图标文件到不同尺寸目录
echo "复制图标文件..."
ICON_SRC="/media/dp25/DATA/deb/fcitx5-deb/gambling-memories/app.gambling-memories/icon/gambling-memories.png"
if [ -f "$ICON_SRC" ]; then
    # 复制到不同尺寸的目录（实际项目中应该包含对应尺寸的图标文件）
    cp "$ICON_SRC" "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png"
    cp "$ICON_SRC" "$PACKAGE_DIR/usr/share/icons/hicolor/22x22/apps/gambling-memories.png"
    cp "$ICON_SRC" "$PACKAGE_DIR/usr/share/icons/hicolor/32x32/apps/gambling-memories.png"
    cp "$ICON_SRC" "$PACKAGE_DIR/usr/share/icons/hicolor/48x48/apps/gambling-memories.png"
    cp "$ICON_SRC" "$PACKAGE_DIR/usr/share/icons/hicolor/64x64/apps/gambling-memories.png"
    cp "$ICON_SRC" "$PACKAGE_DIR/usr/share/icons/hicolor/128x128/apps/gambling-memories.png"
    cp "$ICON_SRC" "$ICON_DIR/gambling-memories.png"
else
    echo "警告: 图标文件不存在，将创建默认图标"
    # 创建简单的图标文件（实际项目中应该使用真实的PNG图标）
    cat > "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
  <rect width="16" height="16" fill="#4A90E2"/>
  <text x="8" y="12" font-family="Arial" font-size="12" fill="white" text-anchor="middle">GM</text>
</svg>
EOF
    # 复制到其他目录
    cp "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png" "$PACKAGE_DIR/usr/share/icons/hicolor/22x22/apps/gambling-memories.png"
    cp "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png" "$PACKAGE_DIR/usr/share/icons/hicolor/32x32/apps/gambling-memories.png"
    cp "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png" "$PACKAGE_DIR/usr/share/icons/hicolor/48x48/apps/gambling-memories.png"
    cp "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png" "$PACKAGE_DIR/usr/share/icons/hicolor/64x64/apps/gambling-memories.png"
    cp "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png" "$PACKAGE_DIR/usr/share/icons/hicolor/128x128/apps/gambling-memories.png"
    cp "$PACKAGE_DIR/usr/share/icons/hicolor/16x16/apps/gambling-memories.png" "$ICON_DIR/gambling-memories.png"
fi

# 创建启动脚本
echo "创建启动脚本..."
cat > "$BIN_DIR/$PACKAGE_NAME" << 'EOF'
#!/bin/bash
cd /usr/share/gambling-memories
python3 main.py
EOF

chmod +x "$BIN_DIR/$PACKAGE_NAME"

# 创建桌面入口文件
echo "创建桌面入口文件..."
cat > "$APP_DATA_DIR/$PACKAGE_NAME.desktop" << EOF
[Desktop Entry]
Name=博彩记忆
Comment=博彩记忆
Exec=/usr/bin/gambling-memories
Icon=gambling-memories
Terminal=false
Type=Application
Categories=Utility;
EOF

# 创建简单的图标文件（实际项目中应该使用真实的SVG图标）
# echo "创建图标文件..."
# cat > "$ICON_DIR/gambling-memories.svg" << 'EOF'
# <svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128">
#   <circle cx="64" cy="64" r="60" fill="#4A90E2"/>
#   <text x="64" y="70" font-family="Arial" font-size="60" fill="white" text-anchor="middle">GM</text>
# </svg>
# EOF

# 创建控制文件
echo "创建DEBIAN控制文件..."
cat > "$DEBIAN_DIR/control" << EOF
Package: $PACKAGE_NAME
Version: $PACKAGE_VERSION
Architecture: $PACKAGE_ARCH
Maintainer: $MAINTAINER
Depends: python3, python3-pyqt5
Description: $DESCRIPTION
Section: utils
Priority: optional
EOF

# 创建安装后脚本
echo "创建安装后脚本..."
cat > "$DEBIAN_DIR/postinst" << 'EOF'
#!/bin/bash
# 安装后脚本

# 设置权限
chmod +x /usr/bin/gambling-memories

# 更新桌面数据库
if [ -x /usr/bin/update-desktop-database ]; then
    update-desktop-database >/dev/null 2>&1 || true
fi

# 更新图标缓存
if [ -x /usr/bin/gtk-update-icon-cache ]; then
    gtk-update-icon-cache -f /usr/share/icons/hicolor >/dev/null 2>&1 || true
fi

echo "博彩记忆应用已安装完成"
EOF

chmod +x "$DEBIAN_DIR/postinst"

# 创建卸载后脚本
echo "创建卸载后脚本..."
cat > "$DEBIAN_DIR/postrm" << 'EOF'
#!/bin/bash
# 卸载后脚本

# 更新桌面数据库
if [ -x /usr/bin/update-desktop-database ]; then
    update-desktop-database >/dev/null 2>&1 || true
fi

# 更新图标缓存
if [ -x /usr/bin/gtk-update-icon-cache ]; then
    gtk-update-icon-cache -f /usr/share/icons/hicolor >/dev/null 2>&1 || true
fi

echo "博彩记忆应用已卸载"
EOF

# 设置文件权限
echo "设置文件权限..."
find "$PACKAGE_DIR" -type f -exec chmod 644 {} \;
find "$PACKAGE_DIR/usr/share/$PACKAGE_NAME" -name "*.py" -exec chmod 755 {} \;

# 为DEBIAN目录下的维护脚本设置正确的权限
chmod 755 "$DEBIAN_DIR/postinst" "$DEBIAN_DIR/postrm"

# 构建deb包
echo "构建deb包..."
dpkg-deb --build "$PACKAGE_DIR" "${BUILD_DIR}/${PACKAGE_NAME}_${PACKAGE_VERSION}_all.deb"

echo "打包完成: ${BUILD_DIR}/${PACKAGE_NAME}_${PACKAGE_VERSION}_all.deb"